package com.xjgzinfo.usdp.web.rest;

import com.xjgzinfo.usdp.AvueServer4jApp;
import com.xjgzinfo.usdp.config.TestSecurityConfiguration;
import com.xjgzinfo.usdp.domain.BladeVisualConfig;
import com.xjgzinfo.usdp.repository.BladeVisualConfigRepository;
import com.xjgzinfo.usdp.service.BladeVisualConfigQueryService;
import com.xjgzinfo.usdp.service.BladeVisualConfigService;
import com.xjgzinfo.usdp.service.dto.BladeVisualConfigDTO;
import com.xjgzinfo.usdp.service.mapper.BladeVisualConfigMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Integration tests for the {@link BladeVisualConfigResource} REST controller.
 */
@SpringBootTest(classes = {AvueServer4jApp.class, TestSecurityConfiguration.class})
@AutoConfigureMockMvc
@WithMockUser
public class BladeVisualConfigResourceIT {

    private static final Long DEFAULT_VISUAL_ID = 1L;
    private static final Long UPDATED_VISUAL_ID = 2L;
    private static final Long SMALLER_VISUAL_ID = 1L - 1L;

    private static final String DEFAULT_DETAIL = "AAAAAAAAAA";
    private static final String UPDATED_DETAIL = "BBBBBBBBBB";

    private static final String DEFAULT_COMPONENT = "AAAAAAAAAA";
    private static final String UPDATED_COMPONENT = "BBBBBBBBBB";

    @Autowired
    private BladeVisualConfigRepository bladeVisualConfigRepository;

    @Autowired
    private BladeVisualConfigMapper bladeVisualConfigMapper;

    @Autowired
    private BladeVisualConfigService bladeVisualConfigService;

    @Autowired
    private BladeVisualConfigQueryService bladeVisualConfigQueryService;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restBladeVisualConfigMockMvc;

    private BladeVisualConfig bladeVisualConfig;

    /**
     * Create an entity for this test.
     * <p>
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static BladeVisualConfig createEntity(EntityManager em) {
        BladeVisualConfig bladeVisualConfig = new BladeVisualConfig()
                .visualId(DEFAULT_VISUAL_ID)
                .detail(DEFAULT_DETAIL)
                .component(DEFAULT_COMPONENT);
        return bladeVisualConfig;
    }

    /**
     * Create an updated entity for this test.
     * <p>
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static BladeVisualConfig createUpdatedEntity(EntityManager em) {
        BladeVisualConfig bladeVisualConfig = new BladeVisualConfig()
                .visualId(UPDATED_VISUAL_ID)
                .detail(UPDATED_DETAIL)
                .component(UPDATED_COMPONENT);
        return bladeVisualConfig;
    }

    @BeforeEach
    public void initTest() {
        bladeVisualConfig = createEntity(em);
    }

    @Test
    @Transactional
    public void createBladeVisualConfig() throws Exception {
        int databaseSizeBeforeCreate = bladeVisualConfigRepository.findAll().size();
        // Create the BladeVisualConfig
        BladeVisualConfigDTO bladeVisualConfigDTO = bladeVisualConfigMapper.toDto(bladeVisualConfig);
        restBladeVisualConfigMockMvc.perform(post("/api/blade-visual-configs").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(bladeVisualConfigDTO)))
                .andExpect(status().isCreated());

        // Validate the BladeVisualConfig in the database
        List<BladeVisualConfig> bladeVisualConfigList = bladeVisualConfigRepository.findAll();
        assertThat(bladeVisualConfigList).hasSize(databaseSizeBeforeCreate + 1);
        BladeVisualConfig testBladeVisualConfig = bladeVisualConfigList.get(bladeVisualConfigList.size() - 1);
        assertThat(testBladeVisualConfig.getVisualId()).isEqualTo(DEFAULT_VISUAL_ID);
        assertThat(testBladeVisualConfig.getDetail()).isEqualTo(DEFAULT_DETAIL);
        assertThat(testBladeVisualConfig.getComponent()).isEqualTo(DEFAULT_COMPONENT);
    }

    @Test
    @Transactional
    public void createBladeVisualConfigWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = bladeVisualConfigRepository.findAll().size();

        // Create the BladeVisualConfig with an existing ID
        bladeVisualConfig.setId(1L);
        BladeVisualConfigDTO bladeVisualConfigDTO = bladeVisualConfigMapper.toDto(bladeVisualConfig);

        // An entity with an existing ID cannot be created, so this API call must fail
        restBladeVisualConfigMockMvc.perform(post("/api/blade-visual-configs").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(bladeVisualConfigDTO)))
                .andExpect(status().isBadRequest());

        // Validate the BladeVisualConfig in the database
        List<BladeVisualConfig> bladeVisualConfigList = bladeVisualConfigRepository.findAll();
        assertThat(bladeVisualConfigList).hasSize(databaseSizeBeforeCreate);
    }


    @Test
    @Transactional
    public void checkVisualIdIsRequired() throws Exception {
        int databaseSizeBeforeTest = bladeVisualConfigRepository.findAll().size();
        // set the field null
        bladeVisualConfig.setVisualId(null);

        // Create the BladeVisualConfig, which fails.
        BladeVisualConfigDTO bladeVisualConfigDTO = bladeVisualConfigMapper.toDto(bladeVisualConfig);


        restBladeVisualConfigMockMvc.perform(post("/api/blade-visual-configs").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(bladeVisualConfigDTO)))
                .andExpect(status().isBadRequest());

        List<BladeVisualConfig> bladeVisualConfigList = bladeVisualConfigRepository.findAll();
        assertThat(bladeVisualConfigList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void getAllBladeVisualConfigs() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get all the bladeVisualConfigList
        restBladeVisualConfigMockMvc.perform(get("/api/blade-visual-configs?sort=id,desc"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$.[*].id").value(hasItem(bladeVisualConfig.getId().intValue())))
                .andExpect(jsonPath("$.[*].visualId").value(hasItem(DEFAULT_VISUAL_ID.intValue())))
                .andExpect(jsonPath("$.[*].detail").value(hasItem(DEFAULT_DETAIL.toString())))
                .andExpect(jsonPath("$.[*].component").value(hasItem(DEFAULT_COMPONENT.toString())));
    }

    @Test
    @Transactional
    public void getBladeVisualConfig() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get the bladeVisualConfig
        restBladeVisualConfigMockMvc.perform(get("/api/blade-visual-configs/{id}", bladeVisualConfig.getId()))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$.id").value(bladeVisualConfig.getId().intValue()))
                .andExpect(jsonPath("$.visualId").value(DEFAULT_VISUAL_ID.intValue()))
                .andExpect(jsonPath("$.detail").value(DEFAULT_DETAIL.toString()))
                .andExpect(jsonPath("$.component").value(DEFAULT_COMPONENT.toString()));
    }


    @Test
    @Transactional
    public void getBladeVisualConfigsByIdFiltering() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        Long id = bladeVisualConfig.getId();

        defaultBladeVisualConfigShouldBeFound("id.equals=" + id);
        defaultBladeVisualConfigShouldNotBeFound("id.notEquals=" + id);

        defaultBladeVisualConfigShouldBeFound("id.greaterThanOrEqual=" + id);
        defaultBladeVisualConfigShouldNotBeFound("id.greaterThan=" + id);

        defaultBladeVisualConfigShouldBeFound("id.lessThanOrEqual=" + id);
        defaultBladeVisualConfigShouldNotBeFound("id.lessThan=" + id);
    }


    @Test
    @Transactional
    public void getAllBladeVisualConfigsByVisualIdIsEqualToSomething() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get all the bladeVisualConfigList where visualId equals to DEFAULT_VISUAL_ID
        defaultBladeVisualConfigShouldBeFound("visualId.equals=" + DEFAULT_VISUAL_ID);

        // Get all the bladeVisualConfigList where visualId equals to UPDATED_VISUAL_ID
        defaultBladeVisualConfigShouldNotBeFound("visualId.equals=" + UPDATED_VISUAL_ID);
    }

    @Test
    @Transactional
    public void getAllBladeVisualConfigsByVisualIdIsNotEqualToSomething() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get all the bladeVisualConfigList where visualId not equals to DEFAULT_VISUAL_ID
        defaultBladeVisualConfigShouldNotBeFound("visualId.notEquals=" + DEFAULT_VISUAL_ID);

        // Get all the bladeVisualConfigList where visualId not equals to UPDATED_VISUAL_ID
        defaultBladeVisualConfigShouldBeFound("visualId.notEquals=" + UPDATED_VISUAL_ID);
    }

    @Test
    @Transactional
    public void getAllBladeVisualConfigsByVisualIdIsInShouldWork() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get all the bladeVisualConfigList where visualId in DEFAULT_VISUAL_ID or UPDATED_VISUAL_ID
        defaultBladeVisualConfigShouldBeFound("visualId.in=" + DEFAULT_VISUAL_ID + "," + UPDATED_VISUAL_ID);

        // Get all the bladeVisualConfigList where visualId equals to UPDATED_VISUAL_ID
        defaultBladeVisualConfigShouldNotBeFound("visualId.in=" + UPDATED_VISUAL_ID);
    }

    @Test
    @Transactional
    public void getAllBladeVisualConfigsByVisualIdIsNullOrNotNull() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get all the bladeVisualConfigList where visualId is not null
        defaultBladeVisualConfigShouldBeFound("visualId.specified=true");

        // Get all the bladeVisualConfigList where visualId is null
        defaultBladeVisualConfigShouldNotBeFound("visualId.specified=false");
    }

    @Test
    @Transactional
    public void getAllBladeVisualConfigsByVisualIdIsGreaterThanOrEqualToSomething() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get all the bladeVisualConfigList where visualId is greater than or equal to DEFAULT_VISUAL_ID
        defaultBladeVisualConfigShouldBeFound("visualId.greaterThanOrEqual=" + DEFAULT_VISUAL_ID);

        // Get all the bladeVisualConfigList where visualId is greater than or equal to UPDATED_VISUAL_ID
        defaultBladeVisualConfigShouldNotBeFound("visualId.greaterThanOrEqual=" + UPDATED_VISUAL_ID);
    }

    @Test
    @Transactional
    public void getAllBladeVisualConfigsByVisualIdIsLessThanOrEqualToSomething() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get all the bladeVisualConfigList where visualId is less than or equal to DEFAULT_VISUAL_ID
        defaultBladeVisualConfigShouldBeFound("visualId.lessThanOrEqual=" + DEFAULT_VISUAL_ID);

        // Get all the bladeVisualConfigList where visualId is less than or equal to SMALLER_VISUAL_ID
        defaultBladeVisualConfigShouldNotBeFound("visualId.lessThanOrEqual=" + SMALLER_VISUAL_ID);
    }

    @Test
    @Transactional
    public void getAllBladeVisualConfigsByVisualIdIsLessThanSomething() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get all the bladeVisualConfigList where visualId is less than DEFAULT_VISUAL_ID
        defaultBladeVisualConfigShouldNotBeFound("visualId.lessThan=" + DEFAULT_VISUAL_ID);

        // Get all the bladeVisualConfigList where visualId is less than UPDATED_VISUAL_ID
        defaultBladeVisualConfigShouldBeFound("visualId.lessThan=" + UPDATED_VISUAL_ID);
    }

    @Test
    @Transactional
    public void getAllBladeVisualConfigsByVisualIdIsGreaterThanSomething() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        // Get all the bladeVisualConfigList where visualId is greater than DEFAULT_VISUAL_ID
        defaultBladeVisualConfigShouldNotBeFound("visualId.greaterThan=" + DEFAULT_VISUAL_ID);

        // Get all the bladeVisualConfigList where visualId is greater than SMALLER_VISUAL_ID
        defaultBladeVisualConfigShouldBeFound("visualId.greaterThan=" + SMALLER_VISUAL_ID);
    }

    /**
     * Executes the search, and checks that the default entity is returned.
     */
    private void defaultBladeVisualConfigShouldBeFound(String filter) throws Exception {
        restBladeVisualConfigMockMvc.perform(get("/api/blade-visual-configs?sort=id,desc&" + filter))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$.[*].id").value(hasItem(bladeVisualConfig.getId().intValue())))
                .andExpect(jsonPath("$.[*].visualId").value(hasItem(DEFAULT_VISUAL_ID.intValue())))
                .andExpect(jsonPath("$.[*].detail").value(hasItem(DEFAULT_DETAIL.toString())))
                .andExpect(jsonPath("$.[*].component").value(hasItem(DEFAULT_COMPONENT.toString())));

        // Check, that the count call also returns 1
        restBladeVisualConfigMockMvc.perform(get("/api/blade-visual-configs/count?sort=id,desc&" + filter))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(content().string("1"));
    }

    /**
     * Executes the search, and checks that the default entity is not returned.
     */
    private void defaultBladeVisualConfigShouldNotBeFound(String filter) throws Exception {
        restBladeVisualConfigMockMvc.perform(get("/api/blade-visual-configs?sort=id,desc&" + filter))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$").isArray())
                .andExpect(jsonPath("$").isEmpty());

        // Check, that the count call also returns 0
        restBladeVisualConfigMockMvc.perform(get("/api/blade-visual-configs/count?sort=id,desc&" + filter))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(content().string("0"));
    }

    @Test
    @Transactional
    public void getNonExistingBladeVisualConfig() throws Exception {
        // Get the bladeVisualConfig
        restBladeVisualConfigMockMvc.perform(get("/api/blade-visual-configs/{id}", Long.MAX_VALUE))
                .andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    public void updateBladeVisualConfig() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        int databaseSizeBeforeUpdate = bladeVisualConfigRepository.findAll().size();

        // Update the bladeVisualConfig
        BladeVisualConfig updatedBladeVisualConfig = bladeVisualConfigRepository.findById(bladeVisualConfig.getId()).get();
        // Disconnect from session so that the updates on updatedBladeVisualConfig are not directly saved in db
        em.detach(updatedBladeVisualConfig);
        updatedBladeVisualConfig
                .visualId(UPDATED_VISUAL_ID)
                .detail(UPDATED_DETAIL)
                .component(UPDATED_COMPONENT);
        BladeVisualConfigDTO bladeVisualConfigDTO = bladeVisualConfigMapper.toDto(updatedBladeVisualConfig);

        restBladeVisualConfigMockMvc.perform(put("/api/blade-visual-configs").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(bladeVisualConfigDTO)))
                .andExpect(status().isOk());

        // Validate the BladeVisualConfig in the database
        List<BladeVisualConfig> bladeVisualConfigList = bladeVisualConfigRepository.findAll();
        assertThat(bladeVisualConfigList).hasSize(databaseSizeBeforeUpdate);
        BladeVisualConfig testBladeVisualConfig = bladeVisualConfigList.get(bladeVisualConfigList.size() - 1);
        assertThat(testBladeVisualConfig.getVisualId()).isEqualTo(UPDATED_VISUAL_ID);
        assertThat(testBladeVisualConfig.getDetail()).isEqualTo(UPDATED_DETAIL);
        assertThat(testBladeVisualConfig.getComponent()).isEqualTo(UPDATED_COMPONENT);
    }

    @Test
    @Transactional
    public void updateNonExistingBladeVisualConfig() throws Exception {
        int databaseSizeBeforeUpdate = bladeVisualConfigRepository.findAll().size();

        // Create the BladeVisualConfig
        BladeVisualConfigDTO bladeVisualConfigDTO = bladeVisualConfigMapper.toDto(bladeVisualConfig);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restBladeVisualConfigMockMvc.perform(put("/api/blade-visual-configs").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(bladeVisualConfigDTO)))
                .andExpect(status().isBadRequest());

        // Validate the BladeVisualConfig in the database
        List<BladeVisualConfig> bladeVisualConfigList = bladeVisualConfigRepository.findAll();
        assertThat(bladeVisualConfigList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    public void deleteBladeVisualConfig() throws Exception {
        // Initialize the database
        bladeVisualConfigRepository.saveAndFlush(bladeVisualConfig);

        int databaseSizeBeforeDelete = bladeVisualConfigRepository.findAll().size();

        // Delete the bladeVisualConfig
        restBladeVisualConfigMockMvc.perform(delete("/api/blade-visual-configs/{id}", bladeVisualConfig.getId()).with(csrf())
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<BladeVisualConfig> bladeVisualConfigList = bladeVisualConfigRepository.findAll();
        assertThat(bladeVisualConfigList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
